var element = Z.element;
var Box = element.Box;

var KeyCode = Z.Key.KeyCode;

// 菜单类型
var MenuType = {
	SubMenu : 1,
	Action : 2
};

// 注册只触发一次的事件
function eventOnce( eventRegister, func ) {
	var thisEvent = null;
	thisEvent = eventRegister( function( event ) {
				func( event );
				thisEvent.remove();
			} );
}

// 子菜单节点
function SubMenuNode( name, accessKey ) {
	return {
		type : MenuType.SubMenu,
		next : null,
		firstChild : null,
		name : name,
		accessKey : accessKey
	};
}

// 动作节点
function ActionNode( name, action, verify, accessKey ) {
	return {
		type : MenuType.Action,
		next : null,
		action : action,
		name : name,
		accessKey : accessKey
	};
}

// 根据一个指针制作一个菜单
function Menu( setRoot ) {
	var tail = null;

	function setTail( node ) {
		if ( tail === null ) {
			tail = node;
			setRoot( tail );
		}
		else {
			tail.next = node;
			tail = node;
		}
	};

	return {
		addSubMenu : function( name, accessKey ) {
			var subMenuNode = SubMenuNode( name, accessKey );
			setTail( subMenuNode );
			return Menu( function( node ) {
						subMenuNode.firstChild = node;
					} );
		},
		addAction : function( name, action, accessKey ) {
			setTail( ActionNode( name, action, accessKey ) );
		}
	};
}

function bannerMenuItem( name, menu, accessKey ) {
	return {
		name : name,
		menu : menu,
		accessKey : accessKey
	};
}

onLoad( function( body ) {
			var emptyAction = {
				action : function() {
				},
				verify : undefined,
				shortCut : undefined,
				name : "empty"
			};

			var action1 = {
				action : function() {
				},
				verify : undefined,
				shortCut : {
					ctrl : true,
					shift : true,
					alt : true,
					key : KeyCode.A
				},
				name : "empty"
			};

			var myMenu = null;
			var menu = Menu( function( node ) {
						myMenu = node;
					} );
			menu.addAction( "第一项", action1 );
			var subMenu = menu.addSubMenu( "第二项" );
			menu.addAction( "第三项", emptyAction, KeyCode.A );
			subMenu.addAction( "第1项", emptyAction );
			var subSubMenu = subMenu.addSubMenu( "第2项", KeyCode.G );
			subMenu.addAction( "第3项", emptyAction );

			subSubMenu.addAction( "第A项", emptyAction );
			subSubMenu.addAction( "第B项", emptyAction );
			subSubMenu.addAction( "第C项", emptyAction, KeyCode.D );

			var myMenu1 = null;
			var menu1 = Menu( function( node ) {
						myMenu1 = node;
					} );
			menu1.addAction( "第一项", emptyAction );
			var subMenu1 = menu1.addSubMenu( "第二项" );
			menu1.addAction( "第三项", emptyAction );

			subMenu1.addAction( "第1项", emptyAction );
			var subSubMenu1 = subMenu1.addSubMenu( "第2项" );
			subMenu1.addAction( "第3项", emptyAction );
			subSubMenu1.addAction( "第A项", emptyAction );
			subSubMenu1.addAction( "第B项", emptyAction );
			subSubMenu1.addAction( "第C项", emptyAction );

			var bmi = bannerMenuItem;
			var bannerMenu = [bmi( "文件", myMenu, KeyCode.A ), bmi( "编辑", myMenu1, KeyCode.K )];
			// 背景区域
			var backgroundArea = body.area();
			var backgroundBox = Box( backgroundArea, {
						background_color : "#E1E6F6"
					} );

			var myBannerMenu = Z.Menu.drawBannerMenu( {
						area : backgroundArea,
						zIndex : 0,
						left : 0,
						top : 0,
						menu : bannerMenu,
						timer : body.Timer
					} );
			myBannerMenu.accentAccessLabel( true );

			body.onKeydown( function( event ) {
						switch ( event.keyCode ) {
							case KeyCode.DownArrow :
								myBannerMenu.down();
								break;
							case KeyCode.LeftArrow :
								myBannerMenu.backward();
								break;
							case KeyCode.RigthArrow :
								myBannerMenu.forward();
								break;
							case KeyCode.UpArrow :
								myBannerMenu.up();
								break;
							case KeyCode.Enter :
								myBannerMenu.enter();
								break;
							case KeyCode.Esc :
								myBannerMenu.escape();
								break;
							case KeyCode.A :
								break;
						}
					} );

			body.onResize( function( size ) {
						backgroundArea.width( size.width );
						backgroundArea.height( size.height );
						backgroundBox.width( size.width );
						backgroundBox.height( size.height );
						myBannerMenu.width( size.width );
					} );
		} );